var ec2ui_LoadBalancerTreeView = {
    COLNAMES : ['loadbalancer.LoadBalancerName','loadbalancer.CreatedTime','loadbalancer.DNSName','loadbalancer.InstanceId',
                ,'loadbalancer.Lbzone','loadbalancer.Interval','loadbalancer.Target','loadbalancer.HealthyThreshold',
                'loadbalancer.Timeout','loadbalancer.UnhealthyThreshold','loadbalancer.AppCookieStickinessPolicies','loadbalancer.LBCookieStickinessPolicies'],
    treeBox : null,
    selection : null,
    ec2ui_session : null,
    retVal : null,
    arrLocalFiles : new Array(),
    LoadblancerList : new Array(),
    registered : false,

    get rowCount() { return this.LoadblancerList.length;},

    setTree      : function(treeBox)            { this.treeBox = treeBox; },
    getCellText : function(idx, column) {
        if (idx >= this.rowCount) return "";
        var member = column.id.split(".").pop();
        return this.LoadblancerList[idx][member];
    },
    isEditable: function(idx, column)  { return true; },
    isContainer: function(idx)            { return false;},
    isSeparator: function(idx)            { return false; },
    isSorted: function()                    { return false; },

    getImageSrc: function(idx, column) { return ""; },
    getProgressMode : function(idx,column) {},
    getCellValue: function(idx, column) {},
    cycleHeader: function(col) {
        var loadbalancer = this.getSelectedLoadbalancer();
        cycleHeader(
        col,
        document,
        this.COLNAMES,
        this.LoadblancerList);

        this.treeBox.invalidate();
        if (loadbalancer) {
            log(loadbalancer.LoadBalancerName + ": Select this Monitor Instance post sort");
            this.selectByName(LoadBalancer.LoadBalancerName);
        } else {
            log("The selected Monitor Instance is null!");
        }

    },
    sort : function() {
        var LoadBalancer = this.getSelectedLoadbalancer();
        sortView(document, this.COLNAMES, this.LoadblancerList);
        if (LoadBalancer) this.selectByName(LoadBalancer.LoadBalancerName);
    },
    
    selectionChanged: function() {},
    cycleCell: function(idx, column) {},
    performAction: function(action) {},
    performActionOnCell: function(action, index, column) {},
    getRowProperties: function(idx, column, prop) {},
    getCellProperties: function(idx, column, prop) {},
    getColumnProperties: function(column, element, prop) {},
    getLevel : function(idx) { return 0; },
    
    getSelectedLoadbalancer : function() {
      
        var index =  this.selection.currentIndex;
       
        if (index == -1) return null;
       
        return this.LoadblancerList[index];
        
    },
  
    
    selectByName : function(Value) {
        this.selection.clearSelection();
        for(var i in this.LoadblancerList) {
             if (this.LoadblancerList[i].LoadBalancerName == LoadBalancerName) {
                this.selection.select(i);
                this.treeBox.ensureRowIsVisible(i);
                return;
            }
        }
        // In case we don't find a match (which is probably a bug).
        this.selection.select(0);
    },
    
    register: function() {
        if (!this.registered) {
            this.registered = true;
            ec2ui_model.registerInterest(this, 'loadbalancer');
        }
    },

    invalidate: function() {
        this.displayLoadBalancer(ec2ui_session.model.loadbalancer);
    },

    refresh: function() {
        ec2ui_session.controller.describeLoadBalancers();
    },
    
    // Create Load Balancer
    CreateLoadBalancer: function() {
        var retVal = {ok:null};
        window.openDialog(
            "chrome://ec2ui/content/dialog_create_loadbalancer.xul",
            null,
            "chrome,centerscreen,modal",
            ec2ui_session,
            retVal,
            null
           );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        if (retVal.ok) {
                ec2ui_session.controller.CreateLoadBalancer(retVal.LoadBalancerName,retVal.Protocol,retVal.elbport,retVal.instanceport,wrap);
                ec2ui_session.controller.ConfigureHealthCheck(retVal.LoadBalancerName,retVal.pingprotocol,retVal.pingport,retVal.pingpath,retVal.Interval,retVal.Timeout,retVal.HealthyThreshold,retVal.UnhealthyThreshold,wrap);
                ec2ui_session.controller.RegisterInstancesWithLoadBalancer(retVal.LoadBalancerName,retVal.Instances,wrap);
        }
     },
     
    //configure Health Check
     ConfigureHealthCheck: function() {
        var loadbalancer = this.getSelectedLoadbalancer();
        if (loadbalancer == null) return;
        var retVal = {ok:null};
        window.openDialog(
            "chrome://ec2ui/content/dialog_configure_healthcheck.xul",
            null,
            "chrome,centerscreen,modal",
            loadbalancer,
            ec2ui_session,
            retVal            
           );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        if (retVal.ok) {        
        //ec2ui_session.controller.createlb(retVal.LoadBalancerName,retVal.Protocol,retVal.elbport,retVal.instanceport,wrap);
        ec2ui_session.controller.EditHealthCheck(loadbalancer.LoadBalancerName,retVal.Target,retVal.Interval,retVal.Timeout,retVal.HealthyThreshold,retVal.UnhealthyThreshold,wrap);
        }
    },
     
    // Loadbalancer Stickness
    loadbalancerstickiness : function(){
     var loadbalancer = this.getSelectedLoadbalancer();
     if (loadbalancer == null) return;
     var retVal = {ok:null};
        window.openDialog(
            "chrome://ec2ui/content/dialog_loadbalancer_stickness.xul",
            null,
            "chrome,centerscreen,modal",
            loadbalancer,
            ec2ui_session,
            retVal
           );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        if (retVal.ok) {
            if (retVal.CookieName != "" || retVal.CookieExpirationPeriod != "")
            {
            if(retVal.CookieName != "")
                ec2ui_session.controller.CreateAppCookieSP(loadbalancer.LoadBalancerName,retVal.CookieName,wrap);
            else
                ec2ui_session.controller.CreateLBCookieSP(loadbalancer.LoadBalancerName,retVal.CookieExpirationPeriod,wrap); 
            }
            else
            {
                ec2ui_session.controller.DeleteLoadBalancerPolicy(loadbalancer.LoadBalancerName,wrap);
            }
        }
    },
    
    //Register instance with loadbalancer 
    
    RegisterInstancesWithLoadBalancer : function(){
        var loadbalancer = this.getSelectedLoadbalancer();
        if (loadbalancer == null) return;
        var retVal = {ok:null};
         window.openDialog(
            "chrome://ec2ui/content/dialog_register_instancelb.xul",
            null,
            "chrome,centerscreen,modal",
            ec2ui_session,
            retVal,
            loadbalancer
            );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        if (retVal.ok) {
        alert(retVal.LoadBalancerName);
        alert(retVal.Instances);
        ec2ui_session.controller.RegisterInstancesWithLoadBalancer(retVal.LoadBalancerName,retVal.Instances,wrap);
        }
    },
    
    
    configureazone : function(){
        var retVal = {ok:null};
        window.openDialog(
            "chrome://ec2ui/content/dialog_configure_azone.xul",
            null,
            "chrome,centerscreen,modal",
            ec2ui_session,
            retVal,
            null
        ); 
    },
    
    //view More Details
    viewDetails : function(){
        var loadbalancer = this.getSelectedLoadbalancer();
        if (loadbalancer == null) return;
        window.openDialog(
            "chrome://ec2ui/content/dialog_loadbalancer_details.xul",
            null,
            "chrome,centerscreen,modal",
            loadbalancer);
    },
    
    // Delete Load balancer
    DeleteLoadBalancer : function(){
        var loadbalancer = this.getSelectedLoadbalancer();
        if (loadbalancer == null) return;
        var confirmed = confirm("Delete Loadbalancer "+loadbalancer.LoadBalancerName+"?");
        if (!confirmed)
            return;
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        ec2ui_session.controller.DeleteLoadBalancer(loadbalancer.LoadBalancerName, wrap);     
    },

    
    //copy to clip board
    copyToClipBoard : function(fieldName) {
        var loadbalancer = this.getSelectedLoadbalancer();
        if (loadbalancer == null) {
            return;
        }

        copyToClipboard(loadbalancer[fieldName]);
    },

    notifyModelChanged: function(interest) {
        this.invalidate();
    },
    
    UploadServerCertificate :function(){
        var retVal = {ok:null};
        window.openDialog(
            "chrome://ec2ui/content/dialog_UploadServerCertificate.xul",
            null,
            "chrome,centerscreen,modal",
            ec2ui_session,
            retVal,
            null
           );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        if (retVal.ok) {        
            ec2ui_session.controller.uploadservercertificate(retVal.ServerCertificateName,retVal.CertificateBody,retVal.PrivateKey,retVal.Path,wrap);
        }
     },

    displayLoadBalancer : function (LoadblancerList) {
        if (!LoadblancerList) { LoadblancerList = []; }

        this.treeBox.rowCountChanged(0, -this.LoadblancerList.length);
        this.LoadblancerList = LoadblancerList;
        this.treeBox.rowCountChanged(0, this.LoadblancerList.length);
        this.sort();
        this.selection.clearSelection();
    
        if (LoadblancerList.length > 0) {
            this.selection.select(0);
        }
    }
};

ec2ui_LoadBalancerTreeView.register();